<?php
/**
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-6-17
 * Time: 上午11:26
 * 获取手机归属地数据 拼好后传给js t.js
 */


/*处理页面上左右点击的时间
 * @param time_center 例如 2015年6月
 * @param action left or right
 */
if($_GET['time_center']){
    $year = (int)substr($_GET['time_center'],0,4);
    $month = (int)substr($_GET['time_center'],7,2);
    if($_GET['action'] == 'left'){//点击左边
        $time_left = $month-2;
        if($time_left<1) {
            $time_left += 12;
        }
        $time_center = $month-1;
        if($time_center<1) {
            --$year;
            $time_center += 12;
        }
        $time_center = strlen($time_center)==1?'0'.$time_center:$time_center;
        $time_left = strlen($time_left)==1 ? '0'.$time_left:(string)$time_left;
        $time_right = strlen($month)==1 ? '0'.$month:(string)$month;
    }
    elseif($_GET['action'] == 'right'){//点击右边
        $time_right = $month+2;
        if($time_right>12){
            $time_right -= 12;
        }
        $time_center = $month+1;
        if($time_center>12){
            ++$year;
            $time_center -= 12;
        }
        $time_center = strlen($time_center)==1?'0'.$time_center:$time_center;
        $time_left = strlen($month)==1 ? '0'.$month:(string)$month;
        $time_right = strlen($time_right)==1 ? '0'.$time_right:(string)$time_right;
    }
    if($time_left && $time_center && $time_right){
        $time_center = (string)$year.'年'.(string)$time_center.'月';
        exit(json_encode(array(
            'sta'=>'suc',
            'list'=>array(
                'time_left'   =>$time_left,
                'time_center' =>$time_center,
                'time_right'  =>$time_right
            )
        )));
    }
    exit('{"sta":"fail","msg":"uncorrect param"}');
}
/*-------------------处理页面上左右点击的时间------结束-------------*/


if(!$_SESSION['sid'])
    exit('{"sta":"OverTime"}');
//公共头部 发起查询
require_once("/var/www/html/new/conf/le.je");
$le=new go_sql();
$le->connect();
include'/var/www/html/new/d/module/zax/curl_test.php';

//客户自己输入景区名进行搜索时
$ltitle = mysql_real_escape_string($_POST['dname']);
if($ltitle!=''){
    $sql = "select id from uu_land where instr(title,'$ltitle') order by id desc limit 1";
    $GLOBALS['le']->query($sql);
    $GLOBALS['le']->fetch_assoc();
    $_POST['lid'] = $GLOBALS['le']->f('id');
}

$tids = '';
$is_apply = false;
if($_SESSION['dtype']==3){//合并过的终端并且是主体
    $sql = "select preSalerID from pft_terminal_share where nowSalerID={$_SESSION['saccount']}";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $total_salerid .= $row['preSalerID'].',';
    }
    $total_salerid = rtrim($total_salerid,',');
    $sql = "select id from uu_land where salerid in ($total_salerid)";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $lid .= $row['id'].',';
    }
    $lid = rtrim($lid,',');
}elseif($_SESSION['dtype']==2){
    $sql = "select id from uu_land where salerid={$_SESSION['account']} limit 1";
    $GLOBALS['le']->query($sql);
    $GLOBALS['le']->fetch_assoc();
    $lid = $GLOBALS['le']->f('id');
    $total_salerid = $_SESSION['account'];
}
if($lid = $_POST['lid'] ? mysql_real_escape_string($_POST['lid']) : $lid){
    if($lid == 2){//取
        $lid = '';
        $sql = "select id from uu_land where addtime>='{$_POST['btime3']}' and addtime<='{$_POST['etime3']}'";
        $GLOBALS['le']->query($sql);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $lid .= $row['id'].',';
        }
        $lid = rtrim($lid,',');
    }
    //取出景区下面所有的票类来查询
    $sql = "select id from uu_jq_ticket where landid in ($lid)";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $tids .= $row['id'].',';
    }
    $tids = rtrim($tids,',');

    if($_SESSION['dtype']!=3 && $_SESSION['sid']!=1){
        //查询是否是这个景区的供应商
        $sql = "select apply_did from uu_land where id=$lid";
        $GLOBALS['le']->query($sql);
        $GLOBALS['le']->fetch_assoc();
        $apply_did = $GLOBALS['le']->f('apply_did');
        $is_apply = $apply_did == $_SESSION['sid'] ? true:false;
    }elseif($_SESSION['dtype']==3 || $_SESSION['dtype']==2){
        $is_apply = true;
    }
}else{
    $lid=='';
}
$GLOBALS['le']->close();
//echo $tids;exit;
if(strpos($_POST['btime3'],'年')){//手机端的页面 传入的时间是 2015年07月 这个样子的
    $year = (int)substr($_POST['btime3'],0,4);
    $month = (int)substr($_POST['btime3'],7,2);
    $_POST['btime3'] = $year.'-'.$month.'-01';
    $_POST['etime3'] = $year.'-'.$month.'-32';
}
$res = post_curl(
    $_POST['btime3'],
    $_SESSION['sid'],
    $zhixiashi=false,
    $_POST['etime3'],
    $_POST['action'],
    $_POST['line_type'],
    $tids,
    $is_apply,
    $lid,
    $_SESSION['sdtype'],
    $total_salerid
);

echo $res;
exit;

switch ($_POST['action']) {
    case 'map':
        goto map;
        break;
    case 'pie':
        goto pie;
        break;
    case 'line':
        goto line;
        break;
    default:
        goto old_map;
        break;
}

old_map:
$res = json_decode($res,true);
//计算总和  用于指向点的值
foreach ($res['city'] as $k => $v) {
    $total_point['point'] += $v['torder'];
    $res['city'][$k]['city'] = mb_substr($v['city'],0,-1,'UTF-8');
}
$soap = new SoapClient(null,array(
        "location" => "http://localhost/open/openService/pft_insideMX.php",
        "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
);

/*
 * 由于现在的统计 景区数量和手机归属地是分开的 只能模糊的去取一个用户所在地
 * 取20个订单 取出归属地最多的地方作为那天的指向地
*/

$xml_1 = simplexml_load_string($soap->Order_Globle_Search(
    "",$_SESSION['sid'], "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "",
    0, 2, 0, 0, "", 0, 0, 0, "", "",
    "", "", "", "", ""));

$xml_2 = simplexml_load_string($soap->Order_Globle_Search(
    "","", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "",
    0, 2, 0, 0, "", 0, 0, 0, "", "",
    $_SESSION['sid'], "", "", "", ""));
if(!count($xml = array_merge(array($xml_2),array($xml_1))))
    $area_code = 3910;//查询不到订单时 默认福州
else{
    foreach ($xml as $ke => $va) {
        foreach ($va->Rec as $k => $v) {
            $area_codes[(int)$v->UUareacode] = $area_codes[(int)$v->UUareacode] ? 1 : $area_codes[(int)$v->UUareacode]+1 ;
        }
    }
    $area_code = array_search(max($area_codes),$area_codes);//取出订单里面最大的地区值 当成地图上指向的点
}

$sql = "select city from pft_area_code_concat where n_code=$area_code limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$sth = $GLOBALS['le']->f('city');
$total_point['city'] = mb_substr($sth,0,-1,'UTF-8');//去掉最后面的'市'那个字
exit(json_encode(array(
    'sta'=>'suc',
    'list'=>array(
        'city'=>$res['city'],
        'total_point'=>array(
            'city' =>$total_point['city'],
            'point'=>$total_point['point']
        )
    )
)));

line:
echo $res;
exit;
pie:
echo $res;
exit;
map:
echo $res;
exit;


